{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install notebook dependencies\n",
    "import sys\n",
    "!{sys.executable} -m pip install itk-ioscanco tqdm requests itkwidgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from tqdm.notebook import tqdm\n",
    "import os\n",
    "\n",
    "from itkwidgets import view\n",
    "\n",
    "import itk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download example data\n",
    "def download_data(url, filename):\n",
    "    if not os.path.exists(filename):\n",
    "        chunk_size = 32 * 1024\n",
    "        r = requests.get(url, stream=True)\n",
    "        total_size = int(r.headers.get('content-length', 0))\n",
    "        pbar = tqdm(unit=\"B\", unit_scale=True, total=int(total_size))\n",
    "        with open(filename, 'wb') as f:\n",
    "            for chunk in r.iter_content(chunk_size=chunk_size): \n",
    "                if chunk: # filter out keep-alive new chunks\n",
    "                    pbar.update(len(chunk))\n",
    "                    f.write(chunk)\n",
    "                    \n",
    "file_name = 'C0004255.ISQ'\n",
    "file_url = 'https://data.kitware.com/api/v1/file/591e56178d777f16d01e0d20/download'\n",
    "download_data(file_url, file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "imageio = itk.ScancoImageIO.New()\n",
    "image = itk.imread(file_name, imageio=imageio)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1dc71affddad4730ba2778e62534c472",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageSS3; pr…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
